/*
* Copyright (c) 2009 contentismissing.net, artifacts.de
*
* All rights reserved.
*
* Authors: Jens Lembke <j@contentismissing.net>;
*          Michael Markowski <m.markowski@artifacts.de>;
*
* All non-exclusive rights granted to Deutsche Telekom AG Laboratories
*
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
package net.contentismissing.tlabs.cocktailparty.ui
{
	import flash.geom. * ;
	import org.cove.ape. * ;
	public class BoundingBox extends Group
	{
		public var boundingBoxWidth : Number;
		public var boundingBoxHeight : Number;
		private var topLeftParticle : CircleParticle;
		private var topRightParticle : CircleParticle;
		private var bottomLeft : CircleParticle;
		private var bottomRight : CircleParticle;
		private var mySpring1 : SpringConstraint;
		private var mySpring2 : SpringConstraint;
		private var mySpring3 : SpringConstraint;
		private var mySpring4 : SpringConstraint;
		private var springStiffness : Number = 0.004;
		private var myComposite : Composite = new Composite();
		public var x : Number;
		public var y : Number;
		private var physicsGroup : Group;
		private var visible : Boolean;
		private var delta : ApeVector;
		public function BoundingBox(_x : Number , _y : Number , _width : Number , _height : Number , physicsGroup : Group , visible : Boolean = true )
		{
			delta = new ApeVector();
			this.physicsGroup = physicsGroup;
			this.boundingBoxWidth = _width;
			this.boundingBoxHeight = _height;
			this.x = _x;
			this.y = _y;
			this.visible = visible;
			topLeftParticle = new CircleParticle(x , y , 2 , false , 1 , 0);
			topRightParticle = new CircleParticle(x + boundingBoxWidth , y , 2 , false , 1 , 0);
			bottomLeft = new CircleParticle(x , y + boundingBoxHeight , 2 , false , 1 , 0);
			bottomRight = new CircleParticle(x + boundingBoxWidth , y + boundingBoxHeight , 2 , false , 1 , 0);
			/*	
			topLeftParticle.multisample = 100;	
			topRightParticle.multisample = 100;	
			bottomLeft.multisample = 100;	
			bottomRight.multisample = 100;	
			*/
			mySpring1 = new SpringConstraint(topLeftParticle , topRightParticle , springStiffness , true , 1 , 1 , true);
			mySpring2 = new SpringConstraint(topRightParticle , bottomRight , springStiffness , true , 1 , 1 , true);
			mySpring3 = new SpringConstraint(bottomRight , bottomLeft , springStiffness , true , 1 , 1 , true);
			mySpring4 = new SpringConstraint(bottomLeft , topLeftParticle , springStiffness , true , 1 , 1 , true);
			if ( !visible)
			{
				topLeftParticle.sprite.visible = false;
				topRightParticle.sprite.visible = false;
				bottomLeft.sprite.visible = false;
				bottomRight.sprite.visible = false;
				mySpring1.sprite.visible = false;
				mySpring2.sprite.visible = false;
				mySpring3.sprite.visible = false;
				mySpring4.sprite.visible = false;
			}

			physicsGroup.addParticle(topLeftParticle);
			physicsGroup.addParticle(topRightParticle);
			physicsGroup.addParticle(bottomLeft);
			physicsGroup.addParticle(bottomRight);
			physicsGroup.addConstraint(mySpring1);
			physicsGroup.addConstraint(mySpring2);
			physicsGroup.addConstraint(mySpring3);
			physicsGroup.addConstraint(mySpring4);
			/*
			myComposite.addParticle(topLeftParticle);
			myComposite.addParticle(topRightParticle);
			myComposite.addParticle(bottomLeft);
			myComposite.addParticle(bottomRight);
			myComposite.addConstraint(mySpring1);
			myComposite.addConstraint(mySpring2);
			myComposite.addConstraint(mySpring3);
			myComposite.addConstraint(mySpring4);
			physicsGroup.addComposite(myComposite);
			*/
		}

		public function rotateByRadian(particel : AbstractParticle , angleRadians : Number , center : ApeVector) : void
		{
			var radius : Number = particel.center.distance(center);
			var angle : Number = getRelativeAngle(center , particel.center) + angleRadians;
			particel.px = (Math.cos(angle) * radius) + center.x;
			particel.py = (Math.sin(angle) * radius) + center.y;
		}

		/**
		* Rotates the Composite to an angle specified in degrees, around a given center
		*/
		public function rotateByAngle(particel : AbstractParticle , angleDegrees : Number , center : ApeVector) : void
		{
			var angleRadians : Number = angleDegrees * (Math.PI / 180);
			rotateByRadian(particel , angleRadians , center);
		}

		private function getRelativeAngle(center : ApeVector , p : ApeVector) : Number
		{
			delta.setTo(p.x - center.x , p.y - center.y);
			return Math.atan2(delta.y , delta.x);
		}

		public function setBox (x : Number , y : Number , width : Number , height : Number , angel : Number , center : ApeVector) : void
		{
			this.x = x;
			this.y = y;
			// TODO: center muss unabhängig von width and height des pockets sein
			//var center:ApeVector = new ApeVector(x+width/2,y+height/2);
			topLeftParticle.px = x;
			topLeftParticle.py = y;
			rotateByAngle(topLeftParticle , angel , center);
			topRightParticle.px = x + width;
			topRightParticle.py = y;
			rotateByAngle(topRightParticle , angel , center);
			bottomLeft.px = x;
			bottomLeft.py = y + height;
			rotateByAngle(bottomLeft , angel , center);
			bottomRight.px = x + width;
			bottomRight.py = y + height;
			rotateByAngle(bottomRight , angel , center);
		}
	}
}
